package com.tim.app.ui.fragment.selceted;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.application.library.base.BaseFragment;
import com.application.library.runtime.event.EventManager;
import com.application.library.widget.smarttab.SmartTabLayout;
import com.lzy.okhttputils.OkHttpUtils;
import com.tim.app.R;
import com.tim.app.constant.AppKey;
import com.tim.app.constant.EventTag;
import com.tim.app.ui.adapter.TabAdapter;

/**
 * 回答页面
 */
public class QuestionAndAnswerFragment extends BaseFragment implements View.OnClickListener {

    public static final String TAG = "QuestionAndAnswerFragment";
    private View rootView;

    private SmartTabLayout tab_layout;
    private android.support.v4.view.ViewPager vpAnswer;
    private TabAdapter pagerAdapter;

    private QuestionAndAnswerListFragment goodAtFragment;
    private QuestionAndAnswerListFragment newestFragment;
    private QuestionAndAnswerListFragment hotFragment;
    private QuestionAndAnswerListFragment inviteFragment;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (null == rootView) {
            rootView = inflater.inflate(R.layout.fragment_answer, container, false);
            rootView.findViewById(R.id.tvDraft).setOnClickListener(this);
            rootView.findViewById(R.id.ibBack).setOnClickListener(this);

            tab_layout = (SmartTabLayout) rootView.findViewById(R.id.tab_layout);
            vpAnswer = (ViewPager) rootView.findViewById(R.id.vpAnswer);

            pagerAdapter = new TabAdapter(getActivity().getSupportFragmentManager());

            goodAtFragment = QuestionAndAnswerListFragment.newInstance(AppKey.PROBLEM_TYPE_RECOMMAND);
            newestFragment = QuestionAndAnswerListFragment.newInstance(AppKey.PROBLEM_TYPE_NEWEST);
            hotFragment = QuestionAndAnswerListFragment.newInstance(AppKey.PROBLEM_TYPE_HOT);
            inviteFragment = QuestionAndAnswerListFragment.newInstance(AppKey.PROBLEM_TYPE_BE_INVIED);
            pagerAdapter.addFragment(goodAtFragment, getString(R.string.answer_tab_title_1));
            pagerAdapter.addFragment(newestFragment, getString(R.string.answer_tab_title_2));
            pagerAdapter.addFragment(hotFragment, getString(R.string.answer_tab_title_3));
            pagerAdapter.addFragment(inviteFragment, getString(R.string.answer_tab_title_4));
            vpAnswer.setOffscreenPageLimit(3);
            vpAnswer.setAdapter(pagerAdapter);
            vpAnswer.setCurrentItem(0);
            tab_layout.setViewPager(vpAnswer);
        }
        return rootView;
    }

    /**
     * 初始化数据
     */
    public void initData() {
        if (null != goodAtFragment) {
            goodAtFragment.initData();
        }
        if (null != newestFragment) {
            newestFragment.initData();
        }
        if (null != hotFragment) {
            hotFragment.initData();
        }
        if (null != inviteFragment) {
            inviteFragment.initData();
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
    }

    @Override
    public void onAttachFragment(Fragment childFragment) {
        super.onAttachFragment(childFragment);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (null != rootView && null != rootView.getParent()) {
            ((ViewGroup) rootView.getParent()).removeView(rootView);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        OkHttpUtils.getInstance().cancelTag(TAG);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvDraft:
                break;
            case R.id.ibBack:
                EventManager.ins().sendEvent(EventTag.GOTO_SELECTED, 0, 0, null);
                break;
            default:
                break;
        }
    }


    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }


}
